

package com.bei_en.teacher.management.controller.feedback;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.feedback.condition.FeedbackCondition;
import com.bei_en.teacher.admin.model.feedback.vo.FeedbackVo;
import com.bei_en.teacher.admin.model.user.condition.UserFinanceCondition;
import com.bei_en.teacher.admin.model.user.vo.UserFinanceVo;
import com.bei_en.teacher.admin.service.feedback.FeedbackService;
import com.bei_en.teacher.admin.service.user.UserFinanceService;
import com.bei_en.teacher.management.controller.BaseController;
import com.bei_en.teacher.management.model.system.vo.SystemUserVo;
import com.bei_en.teacher.management.web.MyJsonFailResult;
import com.bei_en.teacher.management.web.MyJsonResult;
import com.bei_en.teacher.management.web.MyJsonSuccessResult;

/**
 * 
 * @author Guocg
 * @date 2016-11-26 10:09:53
 */
@Controller
@RequestMapping("/admin")
public class FeedbackController extends BaseController {
	@Autowired
	UserFinanceService userFinanceService;
    @Autowired
    FeedbackService feedbackService;
    private void addSharedAttr(Model model) {
    	model.addAttribute("StatusType", gson.fromJson("{1:'待审核',2:'已审核'}", Map.class));
    }

    @RequestMapping("/feedback")
    public String feedback(Model model,FeedbackCondition vo, HttpServletResponse resp ) {

		List<FeedbackVo> list =new ArrayList<FeedbackVo>();
		try {
		    list = feedbackService.findFeedbackPage(vo);
		} catch (Exception e) {
		    e.printStackTrace();
		    writeJson(resp, new MyJsonFailResult(e.getMessage()));
		    return null;
		}
	
		model.addAttribute("list", list);
		addSharedAttr(model);
		model.addAttribute("vo", vo);
	
		return "/admin/feedback/feedbackList.html";
    }

    @RequestMapping("/feedbackAdd")
    public String feedbackAdd(Model model, FeedbackVo vo, boolean show, HttpSession session,
	    HttpServletResponse resp) {

	if (show) {
	    addSharedAttr(model);
	    return "/admin/feedback/feedbackAdd.html";
	}
	try {
	    SystemUserVo obj = getLoginUser(session);
	    if (obj!=null)
		vo.setUpdateId(obj.getId());
	    else
		vo.setUpdateId(0);
	    vo.setCreateTime(new Date());
	feedbackService.insert(vo);
	} catch (Exception e) {
	    e.printStackTrace();
	    writeJson(resp, new MyJsonFailResult(e.getMessage()));
	    return null;
	}
	MyJsonSuccessResult re = new MyJsonSuccessResult();
	re.setCloseCurrent(true);
	re.setTabid("feedbackList");
	writeJson(resp, re);
	return null;
    }

    @RequestMapping("/feedbackEdit")
    public String feedbackEdit(Model model,boolean show, FeedbackVo vo, HttpSession session, HttpServletResponse resp) {
	
		if (show) {
		    FeedbackVo vo1 = feedbackService.findById(vo.getId());
		    addSharedAttr(model);
		    model.addAttribute("vo", vo1);
		    return "/admin/feedback/feedbackEdit.html";
		}
		try {
		    SystemUserVo obj = getLoginUser(session);
		    if (obj!=null)
			vo.setUpdateId(obj.getId());
		    else
			vo.setUpdateId(0);
		    vo.setUpdateTime(new Date());
		feedbackService.updateSelective(vo);
		
		
		UserFinanceCondition condition = new UserFinanceCondition();
		condition.setUserId(vo.getUserId());
		
		
		List<UserFinanceVo> userFinanceVos = userFinanceService
				.findUserFinancePage(condition);
		UserFinanceVo userFinance = new UserFinanceVo();
		userFinance.setUserId(vo.getUserId());
		userFinance.setUserIds(0);
		if(vo.getteacher()!=null){
			userFinance.setMoney(vo.getteacher());
		}
		userFinance.setType(1);
		userFinance.setSource(7);
		userFinance.setStatus(1);
		userFinance.setCreateTime(new Date());
		if (!userFinanceVos.isEmpty()) {
			userFinance.setRemaining(userFinanceVos.get(0).getRemaining()
					+ vo.getteacher());

		} else {
			userFinance.setRemaining(vo.getteacher());
		}
		switch (userFinance.getSource()) {
		case 1:
			userFinance.setNote("点赞");
			break;
		case 2:
			userFinance.setNote("分享");
			break;
		case 3:
			userFinance.setNote("收藏");
			break;
		case 4:
			userFinance.setNote("评论");
			break;
		case 7:
			userFinance.setNote("反馈奖励");
			break;
		}
		int id = userFinanceService.merge(userFinance);
		
		} catch (Exception e) {
		    e.printStackTrace();
		    writeJson(resp, new MyJsonFailResult(e.getMessage()));
		    return null;
		}
		MyJsonSuccessResult re = new MyJsonSuccessResult();
		re.setCloseCurrent(true);
		re.setTabid("feedbackList");
		writeJson(resp, re);
		return null;
    }

    @RequestMapping("/feedbackDel")
    @ResponseBody
    public MyJsonResult feedbackDel(Model model, int[] delids, HttpSession session) {

		try {
		    for (int i : delids) {
			FeedbackVo vo1 = new FeedbackVo();
			vo1.setId(i);
//			vo1.setStatus(0);
			/*SysUserVo obj = getLoginUser(session);
			if (obj!=null)
			    vo1.setUpdateId(obj.getId());
			else
			    vo1.setUpdateId(0);*/
			vo1.setUpdateTime(new Date());
			feedbackService.updateSelective(vo1);
		    }
		} catch (Exception e) {
		    e.printStackTrace();
		    return new MyJsonFailResult(e.getMessage());
		}
		return new MyJsonSuccessResult();
	}
    
}
